<?php
/**
 * Framework_Util_HtmlFormRedirect
 *
 * Redirect the web browser by use of a form on an html page with an
 * onload behavior to automatically submit it.
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @package BAMF
*/

abstract class Framework_Util_HtmlFormRedirect {

    /**
     * doRedirect
     *
     * Generate an html page with a form that submits when the page loads
     *
     * @author Application Support Group
     * @access public
     * @param string $formAction The action for the form. Defaults to empty string.
     * @param string $formMethod Either "GET" or "POST" (defaults to "GET")
     * @param array $data Hash of data to pass to the next page
     * @return void
    */


	public static function doRedirect( $formAction="", $formMethod="get", $data=array() ) {
	
		print "\n<html><head><title>...redirecting...</title></head>" ;
		print "\n<body onload='document.getElementById(\"redirectForm\").submit()'>" ;
		print "\n<form id='redirectForm' action='$formAction' method='$formMethod'>" ;
	
		Framework_Util_HtmlFormRedirect::printRequestArray( $data ) ;
	
		print "\n</form>" ;
		print "\n</body>" ;
		print "\n</html>" ;
	}

	
	private static function printRequestArray( $theArray, $useName=null ) {

		foreach( $theArray as $key => $value) {
			if( is_array( $value )) {
				Framework_Util_HtmlFormRedirect::printRequestArray( $value, $key ) ;
			} else {
				if( !empty( $useName )) {
					$reqName = $useName."[".$key."]" ;
					print("\n<input type='hidden' name='$reqName' value='$value' />");
				} else {
					print("\n<input type='hidden' name='$key' value='$value' />");
				}
			}
		}

		return ;
	}

}

?>
